<?php
/**
 * Class is used to define basic overall functions in the underlying classes
 * @author Hugo Zonderland
 *
 */
class ClassBase {
	
	public $class;
	public $tablename;
	public $mysqlInstance;
	public $fields;
	
	/**
	 * Default construct
	 */
	public function __construct() {
		$this->mysqlInstance = mysql_connect("localhost", "root", "");
		$this->mysqlInstance = mysql_select_db("romantics");
	}
	
	/**
	 * uniform getById class
	 * @param int $id
	 * @return $object of any time
	 */
	public function get($id) {
		
		$object = new $this->class();
		$query = mysql_real_escape_string("SELECT * FROM `" + $this->tablename + "` WHERE id == " + $id);
		$results = mysql_query($query, $mysqlInstance);
		if (mysql_numrows($results) > 0) {
			$result = @mysql_fetch_assoc($results);
			foreach ($object->fields as $field => $key) {
				$object->$key = $result[$key];
			}	
		}
		return $object;
	}
	
	/**
	 * Saves an object to the database
	 * @param Object $object
	 */
	public function save($object) {	
		// Start the mysql query.
		$mysql = "INSERT INTO `" . $this->tablename . "` (";
		
		// Loop trough the objects field and determen if their empty
		// if not empty, add the field to the insert query
		foreach ($object->fields as $field => $key) {
			
			$val = $object->$key;
			
			// Detirmen the value's type and check if its set or not.
			if (is_string($val)) {
				if ($val != null) {
					$mysql.= $key . ",";
				}
			} else {
				if ($val != 0) {
					$mysql.= $key . ",";
				}		
			}				
		}
		
		// Remove the last ',' of the mysql query and add a ') VALUES ('
		$mysql = substr($mysql, 0, strlen($mysql) -1) . ") VALUES (";
		
		// Loop trough the values of the fields again and deterimen of their empty
		// if not empty add the fiels value to the VALUE part of the query
		foreach ($object->fields as $field => $key) {
			$val = $object->$key;
			if (is_string($val)) {
				if ($val != null) {
					$mysql .= "\"" . mysql_real_escape_string($val) . "\", ";
				}
			} else {
				if ($val != 0) {
					$mysql .= "" . mysql_real_escape_string($val) . ", ";
				}
			}
		}
		
		// Remove the last ', ' and add ');' to close the query
		$mysql = substr($mysql, 0, strlen($mysql) -2) . ");";
		print $mysql;
	}
	
	/**
	 * Remove an object from the database
	 * @param $object to get deleted
	 */
	public function delete($object) {
		
		// Get the object from the database
		$object = get($object->id);
		
		// Check if the object excisist
		if ($object != null) {
			
			// Define the mysql query
			$mysql = "DELETE FROM `" . $this->tablename . "` WHERE id == " . $object->id . ";";
			
			print $mysql;
		}
	}
	
	/**
	 * Updates an object in the database
	 * @param $object
	 */
	public function update($object) {
		
		// Initiates the query
		$query = "UPDATE `" . $this->tablename ."` SET ";
		
		// Get the object in its old state.
		$objectOldState = get($object->id);
		
		// Compare the current variables of the old object to the new object
		// to figure out what needs to be updated
		foreach ($object->fields as $key => $field) {
			
			// Compare the new object variable value with the old one.
			if ($object->$field != $objectOldState->$field) {
				// Check if the value is a string, needs escaping..
				if (is_string($object->$field)) {
					$query .= $field . " = \"" . $object->$field . "\", "; 
				} else {
					// Integers or other values wont need escaping.
					$query .= $field . " = ". $object->$field . ", ";
				}
			}
		}
		
		// Remove the last ' ,' and set ';' at the end
		$query = substr($query, 0, strlen($query) - 2) . ";";
		print $query;
	}
}